import React, { Component } from 'react'
import { Modal, message } from 'antd';
import {ListBox} from '../../../components'
import Actions from '../ActionStore/Actions';
import store from '../ActionStore/Stores';
import { fetchData } from '../../../components/AjaxConsumer'

export default class ListProductMainPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            url: '',
            expCondition: ''
        };

        //行操作列表
        this.rowOperation = [
            // {
            //     handleName: 'edit',
            //     iconClass: 'cellEdit'
            // },{
            //     handleName: 'delete',
            //     iconClass: 'cellDele'
            // },{
            //     handleName: 'assignRole',
            //     iconClass: 'cellRole'
            // }
            {
                handleName: 'doubleClick',
                showName: '行双击事件'
            }
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'addUser',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },
            /*{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            },*/
            {
                handleName: 'info',
                showName: '查看',
                iconClass: 'info'
            },
            {
                handleName: 'export',
                showName: '导出',
                iconClass: 'export'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'ListUserColumnConfig',
            data: [
                {
                    showName: 'productId',
                    columnName: 'productId',
                    columnHidden:true,
                    foreverHidden: true
                },
                {
                    showName: '产品编码',
                    columnName: 'productCode',
                    columnWidth: 130
                },{
                    showName: '中文描述',
                    columnName: 'productDescZh'
                },{
                    showName: '英文描述',
                    columnName: 'productDescEn'
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate'
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy'
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified'
                }/*,{
                 showName: '有效标志',
                 columnName: 'activeFlag'
                 }*/
            ]
        };

        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        this.searchComponentData = [
            {
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin'
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax',
                minName:'gmtModifiedMin'
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'addUser'){
            this.addUserHandle();
        }else if(strHandles == 'info'){
            this.infoHandle(rowObj);
        }else if(strHandles == 'doubleClick'){
            this.doubleClickHandle(rowObj);
        }else if(strHandles == 'export'){
            this.setState({
                expCondition: rowObj
            });

            this.exportHandle(rowObj);
        }
    }

    expConfirmCallBack =(resData)=>{
        console.log("提交返回数据：\n",JSON.stringify(resData, null, 4));
        if(resData.code == 1){
            let conditionStr = '';
            for(let key in this.state.expCondition){
                conditionStr = conditionStr + ' and ' + key.replace(/([A-Z])/g,"_$1").toLowerCase() + '=\'' + conditonData[key] + '\'';
            }

            //单表导出
            window.location.href='/base/excel/common/exp' +
                '?fileName=产品档案.xlsx' +
                '&tableName=md_product' +
                    // '&whereCondition=product_id=\'1\'and product_code=\'1\'';
                '&whereCondition=' + conditionStr;
        }
    }

    //导出excel
    exportHandle = (conditonData) => {
        //alert('conditonData: ' + JSON.stringify(conditonData, null, 4));
        //this.refs.ifile.props.src='/base/product/exp1';
        //this.setState({
        //    url: '/base/product/exp'
        //});

        let conditionStr = '';
        for(let key in conditonData){
            conditionStr = conditionStr + ' and ' + key.replace(/([A-Z])/g,"_$1").toLowerCase() + '=\'' + conditonData[key] + '\'';
        }

        //单表导出=
        let url ='/base/excel/common/expConfirm' +
            '?tableName=md_product' +
                // '&whereCondition=product_id=\'1\'and product_code=\'1\'';
            '&whereCondition=' + conditionStr;

        const options= {
            url: url,
            type: 'get',//请求方式
            condition:{}
        };
        fetchData(this, options, this.expConfirmCallBack);


        //多表关联导出
        //window.location.href='/base/excel/expdic';

        //window.open('/base/product/exp');

        //$.ajax({a=
        //    url: '/base/product/exp1',1&
        //    type: 'get',
        //    //dataType: 'json',
        //    //contentType:'application/x-www-form-urlencoded; charset=utf-8',
        //    //data: {},
        //    success: function () {
        //        alert("111111111111111");
        //    },
        //    error: function (XMLHttpRequest, textStatus, errorThrown) {
        //        alert("22222222222");
        //    }
        //});
    }


    infoHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            Actions.rowDoubleClick('查看', rowObj);
        }
    }
    doubleClickHandle = (rowObj) => {
        //双击行显示明细，跳转到明细tab，‘2’是定死的key值
        Actions.rowDoubleClick('编辑', rowObj);
    }

    // 增加用户
    addUserHandle = () => {
        Actions.rowDoubleClick('新建', null);
    }
    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            Actions.rowDoubleClick('编辑', rowObj);
        }
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateUser.deleteHandle(rowObj[0].data)
        }
    }

    componentWillReceiveProps(nextProps) {
        //刷新当前列表
        if(nextProps.refresh){
            this.refs.listProducts.refreshHandle();
        }
    }
    //
    ////刷新列表
    //refreshHandle = () => {
    //    alert('12345');
    //    this.refs.listProducts.refreshHandle();
    //}

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <div>
                <ListBox
                    ref='listProducts'
                    isHideLeft={isHideLeft}
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/product/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    subtractHeight = '37'
                    subtractWidth ='0'
                    matchedRows ={this.props.matchedRows}
                    isRowSingle = "true"
                />
            </div>
        )
    }
}
